//编辑地图主文件
import EGlobal from "./EGlobal"
cc.Class({
    extends: cc.Component,

    properties: {
        P_blockWidth: {
            default: 106,
            tooltip: "每个地图块的宽度",
        },
        P_blockHeight: {
            default: 106,
            tooltip: "每个地图块的高度",
        },
        P_xNumber: {
            default: 4,
            tooltip: "每一行显示几个"
        },
        P_blockAtllas: {
            default: null,
            type: cc.SpriteAtlas,
            tooltip: "地图块的图集"
        },
        P_editView: {
            default: null,
            type: cc.Node,
            tooltip: "选择的区域父级"
        },
        P_editNode: {
            default: null,
            type: cc.Node,
            tooltip: "选择的区域和地图的区域"

        },
        P_viewMap: {
            default: null,
            type: cc.Node,
            tooltip: "生成的地图节点"
        },
        P_viewPath: {
            default: null,
            type: cc.Node,
            tooltip: "生成的路径节点"
        },
        P_pathBlock: {
            default: null,
            type: cc.Prefab,
            tooltip: "路径块prefab"
        },


    },

    onLoad() {
        EGlobal.mapEdit = this;
        this.creatMoveImage();
        this.creatAllAtllsa();
        this.creatViewMap();
        this.P_editNode.on('touchstart', this.touchstart, this)
        this.P_editNode.on('touchmove', this.touchmove, this)
        this.P_editNode.on('touchend', this.touchend, this)
        this.P_editNode.on('touchcancel', this.touchcancel, this)
        this.map_control = this.node.getComponent("map_control")
        this.path_control = this.node.getComponent("path_control")
    },
    ctor() {
        this.creatBlocks = []; //所有创建出来的块（为了判断选了哪个）
        this.moveImage = null; //拖拽的照片
        this.viewMapItems = [
            [4, 4, 4, 4, 4, 4],
            [4, 4, 4, 4, 4, 4],
            [4, 4, 4, 4, 4, 4],
            [4, 4, 4, 4, 4, 4],
            [4, 4, 4, 4, 4, 4],
            [4, 4, 4, 4, 4, 4]
        ] //右边生成的地图item
    },
    creatMoveImage() {
        //生成拖拽照片
        let node = new cc.Node();
        node.active = false;
        node.zIndex = 100;
        node.addComponent(cc.Sprite);
        this.P_editNode.addChild(node);
        this.moveImage = node;
    },
    creatAllAtllsa() {
        //创建所有图集
        let atllasView = this.P_editNode.getChildByName("atllasView")
        this.P_editNode.addComponent(cc.Sprite)
        let spriteAtlas = this.P_blockAtllas.getSpriteFrames(); //获取所有图集数组
        for (let i = 0; i < spriteAtlas.length; i++) {
            let block = new cc.Node; //创建一个节点
            atllasView.addChild(block); //把创建的节点添加在mapView节点上
            let component = block.addComponent(cc.Sprite); //在创建的节点上添加Sprite组件
            let frame = this.P_blockAtllas.getSpriteFrame(i + 1); //获取地图块的图集的一张图片
            component.spriteFrame = frame; //块组件的照片赋值
            block.x = (i % this.P_xNumber * this.P_blockWidth) + (i % this.P_xNumber * 5) + (this.P_blockWidth / 2);
            //每一个的x坐标                            //间距                   //以为是中心点（0。5，0.5） 要变成（0，0）
            block.y = -parseInt(i / this.P_xNumber) * this.P_blockHeight - parseInt(i / this.P_xNumber) * 5 - (this.P_blockHeight / 2)
            //- 往下排   //parseInt(i / this.P_xNumber) i是第几行 * this.P_blockHeight=y坐标 //  parseInt(i / this.P_xNumber)*3三被间距 //以为是中心点（0。5，0.5） 要变成（0，0）
            this.creatBlocks[i] = block //存储所有的block
        }
    },
    touchstart(event) {
        if (EGlobal.editType == "编辑地图") {
            this.map_control.mapTouchstart(event)
        } else {
            this.path_control.pathTouchstart(event)
        }

    },
    touchmove(event) {
        if (EGlobal.editType == "编辑地图") {
            this.map_control.mapTouchmove(event)
        } else {
            //    this.path_control.pathTouchstart(event)
        }
    },
    touchend(event) {
        if (EGlobal.editType == "编辑地图") {
            this.map_control.mapTouchend(event)
        } else {
            // pathTouchstart(event)
        }
    },
    touchcancel(event) {
        if (EGlobal.editType == "编辑地图") {
            this.map_control.mapTouchcancel(event)
        } else {
            // pathTouchstart(event)
        }
    },
    getCurBlock(wordPos) {
        //相当于你把你要托拽的点放在这个blok[i]内
        for (let i = 0; i < this.creatBlocks.length; i++) {
            let pos = this.creatBlocks[i].convertToNodeSpaceAR(wordPos)
            if (pos.x >= -this.P_blockWidth / 2 && pos.x <= this.P_blockWidth / 2) {
                //在块的范围内x轴内  ➗2 因为是（0.5，0.5）
                if (pos.y >= -this.P_blockHeight / 2 && pos.y <= this.P_blockHeight / 2) {
                    //在块的范围内Y轴内
                    return i

                }
            }
        }
        return null
    },
    creatViewMap() {
        // let scene = cc.director.getScene();
        let xBlockNumber = parseInt(this.P_viewMap.width / this.P_blockWidth); //x轴块的数量
        let yBlockNumber = parseInt(this.P_viewMap.height / this.P_blockHeight); //Y轴块的数量
        for (let i = 0; i < yBlockNumber; i++) {
            let y = this.P_blockHeight * i; //y轴坐标
            for (let o = 0; o < xBlockNumber; o++) {
                let x = this.P_blockWidth * o; //x轴坐标
                let posx = x + this.P_blockWidth / 2 //因为每一个块的锚点在中心(0.5,0.5) 所以哑巴他的中信移到(0,1)
                let posy = -y - this.P_blockHeight / 2 //每一个块的坐标点 -y 是因为mapView 的锚点在(0,1) 向下y就要是负
                let pos = cc.v2(posx, posy);
                let block = new cc.Node; //创建一个节点
                this.P_viewMap.addChild(block); //把创建的节点添加在mapView节点上
                let component = block.addComponent(cc.Sprite); //在创建的节点上添加Sprite组件
                var frame = this.P_blockAtllas.getSpriteFrame(this.viewMapItems[i][o]); //获取地图块的图集的一张图片
                component.spriteFrame = frame; //块组件的照片赋值
                block.setPosition(pos); //设置块的位置
                this.viewMapItems[i][o] = block
            }
        }
    },
    moveToViewMap(pos) {
        //把选中的block放在viewMap里
        // let
    },
    getCurMapBlock(wordPos) {
        //获取右边地图拖到了那个格
        let xBlockNumber = parseInt(this.P_viewMap.width / this.P_blockWidth); //x轴块的数量
        let yBlockNumber = parseInt(this.P_viewMap.height / this.P_blockHeight); //Y轴块的数量

        for (let a = 0; a < this.viewMapItems.length; a++) {
            for (let i = 0; i < yBlockNumber; i++) {
                for (let o = 0; o < xBlockNumber; o++) {
                    let pos = this.viewMapItems[i][o].convertToNodeSpaceAR(wordPos)
                    if (pos.x >= -this.P_blockWidth / 2 && pos.x <= this.P_blockWidth / 2) {
                        //在块的范围内x轴内  ➗2 因为是（0.5，0.5）
                        if (pos.y >= -this.P_blockHeight / 2 && pos.y <= this.P_blockHeight / 2) {
                            //在块的范围内Y轴内
                            return [o, i]
                        }
                    }
                }
            }
        }
        return null
    },
    echoMapData() {
        let echoMapData = []
        let xBlockNumber = parseInt(this.P_viewMap.width / this.P_blockWidth); //x轴块的数量
        let yBlockNumber = parseInt(this.P_viewMap.height / this.P_blockHeight); //Y轴块的数量

        for (let i = 0; i < this.viewMapItems.length; i++) {
            for (let i = 0; i < yBlockNumber; i++) {
                echoMapData[i] = []
                for (let o = 0; o < xBlockNumber; o++) {
                    echoMapData[i][o] = parseInt(this.viewMapItems[i][o].getComponent(cc.Sprite).spriteFrame.name) 
                }
            }
        }
        return JSON.stringify(echoMapData);
    },
    creatPathBlock() {
        //生成路径地图
        this.P_viewPath.removeAllChildren(); //把这个节点上的子节点都删除
        EGlobal.pathList.map((v, i) => { //循环绘制出路径
            let pathBLock = cc.instantiate(this.P_pathBlock);
            pathBLock.getComponent("pathBlock").setNumber(i+1)
            pathBLock.x = v[0] * this.P_blockWidth + this.P_blockWidth / 2
            pathBLock.y = -v[1] * this.P_blockHeight - this.P_blockWidth / 2
            this.P_viewPath.addChild(pathBLock)
        })
    }

});